<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>就诊</title>
    <style>
        body{
            width: calc(100% - 200px);
            background-color: antiquewhite
        }
    </style>
	<script type="text/javascript" src="../js/jquerysession.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
    
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js">
	</script>

</head>
<body>
    <div class="container-fluid">
	<div class="row-fluid">
		<div class="col-md-12">
			<div class="row-fluid">
				<table class="table">
						<thead>
							<tr>
								<th>
									学生编号
								</th>
								<th>
									学生姓名
								</th>
								<th>
									性别
								</th>
								<th>
									年龄
								</th>
								<th>
									系部
								</th>
							</tr>
						</thead>
						<tbody id="tb_st">
							<tr>
								<td>
									1
								</td>
								<td>
									TB - Monthly
								</td>
								<td>
									01/04/2012
								</td>
								<td>
									Default
								</td>
								<td>
								    Default
								</td>
							</tr>

						</tbody>
					</table>
			</div>
		</div>
	</div>
	
	<div class="row-fluid">
	
		<div class="col-md-12">
		<h4>诊断报告</h4>
		<textarea id="dg_info" rows="6" cols="100"></textarea>
		
		</div>
		<button id="save_dg_info" style="float: right">保存</button>
	</div>    
	<div class="row-fluid">
		<div class="col-md-12">
			<div class="row-fluid">
				<div class="col-md-12">
				</div>
			</div>
			<div class="row-fluid">
			
				<div class="col-md-7" style="float: left">
					<h4>药品库</h4>
						<input id="input_med" class="input-medium search-query" type="text" /> <button id="btn_med">查找</button>


					<table class="table">
						<thead>
							<tr>
								<th>
									编号
								</th>
								<th>
									药品名称
								</th>
								<th>
									药品库存
								</th>
								<th>
									药品价格
								</th>
								<th>
									添加
								</th>
							</tr>
						</thead>
						<tbody id="tr_medTable">
							<tr>
								<td id="med_id">
									
								</td>
								<td id="med_name">
									
								</td>
								<td id="med_inventory">
									
								</td>
								<td id="price">
									
								</td>
								<td>
									<button id="add_med">
									    添加
									</button>
								</td>
							</tr>

						</tbody>
					</table>
				</div>
			<div class="col-md-1" style="margin-top: 50px">
			    <p class="text-nowrap">
			        ——>
			    </p>
			</div>
			<h4>患者用药</h4>
				<div class="col-md-4">
					<table class="table">
						<thead>
							<tr>
								<th>
									编号
								</th>
								<th>
									产品
								</th>
								<th>
									价格
								</th>
								
							</tr>
						</thead>
						<tbody id="th_med3">

							
						</tbody>

					</table>
					<button id="clean_all">清除</button>
					<button id="save_med">保存</button>
				</div>
			</div>
		</div>
	</div>
	
</div>
</body>

<script>


    $(function(){

		var st_ID_now;
		var cr_ID = location.search.split('=')[1];


		var med_list = [];

		$("#clean_all").click(function (){
			$("#th_med3").html('');
		});
		var med_str_list;

        $.ajax({
            url:"/medicine/findAll",
            type:"POST",
			data:null,
            success: function(result){
               if (result.state == 200){
					var med = result.data;
					$("#tr_medTable").html('');
					$.each(med,function (i) {
						$tr = $("<tr/>");
						$td1 = $("<td/>").text(med[i].med_ID);
						$td2 = $("<td/>").text(med[i].med_Name);
						$td3 = $("<td/>").text(med[i].med_Inventory);
						$td4 = $("<td/>").text(med[i].price);
						$td5 = $("<button/>").text("添加").click(function () {
							med_list.push(med[i].med_ID);
							console.log(med_list);
							//将数组转换字符串
							med_str_list = med_list.join(',');
							console.log(med_str_list);

							//将添加的数组传入查找Medicine传入表格
							$.ajax({
								url:"/medicine/findById_List",
								type:"POST",
								data:{
									id_list:med_str_list
								},
								success: function (rst){
									if (rst.state == 200){
										var med = rst.data;
										$("#th_med3").html('');
										$.each(med,function (i) {
											console.log(med[i]);
											$tr = $("<tr/>");
											$td1 = $("<td/>").text(med[i].med_ID);
											$td2 = $("<td/>").text(med[i].med_Name);
											$td3 = $("<td/>").text(med[i].price);
											$tr.append($td1).append($td2).append($td3);
											$("#th_med3").append($tr);
										});

									}


								}
							});










						});


						$tr.append($td1).append($td2).append($td3).append($td4).append($td5);

						$("#tr_medTable").append($tr);
					});


			   }

            
        }
    }
              );


		$("#save_med").click(function () {
			alert("test");
			//将添加的内容到数据库
			//根据cr_id更新Diagnosis的数据
			$.ajax({
				url:"/diagnosis/update_List",
				type:"POST",
				data:{
					med_id_list:med_str_list,
					cr_ID:cr_ID
				},
				success: function (rst){
					if (rst.state == 200){
						console.log(rst);
					}
				}
			});
		});





		$.ajax({
					url:"/clinicReg/findById",
					type:"POST",
					data:{cr_ID:cr_ID},
					async:false,
					success: function(result){
						if (result.state == 200){
							st_ID_now = result.data.st_ID;
						}else{
							alert(result.message);
						}

					}
				}
		);


		$.ajax({
					url:"/student/findByID",
					type:"POST",
					data:{st_ID:st_ID_now},

					success: function(result){
						if (result.state == 200){
							var student = result.data;
							$("#tb_st").html('');
							$tr = $("<tr>");
							$td1 = $("<td/>").text(student.st_ID);
							$td2 = $("<td/>").text(student.realName);
							var gender = student.gender;
							var srcGender;
							if(gender ==1 ){
								srcGender = "男";
							}else{
								srcGender = "女";
							}
							$td3 = $("<td/>").text(srcGender);
							$td4 = $("<td/>").text(student.age);
							$td5 = $("<td/>").text(student.department);
							$tr.append($td1).append($td2).append($td3).append($td4).append($td5);

							$("#tb_st").append($tr);


						}


					}
				}
		);








    });

	$("#btn_med").click(function (){
		var input_med = $("#input_med").val();
		$.ajax({
					url:"/medicine/findByName",
					type:"POST",
					data:{med_Name:input_med},
					success: function(result){
						if (result.state == 200){
							$("#tr_medTable").html('');
							$tr = $("<tr/>");
							$td1 = $("<td/>").text(result.data.med_ID);
							$td2 = $("<td/>").text(result.data.med_Name);
							$td3 = $("<td/>").text(result.data.med_Inventory);
							$td4 = $("<td/>").text(result.data.price);
							$td5 = $("<button/>").text("添加");
							$tr.append($td1).append($td2).append($td3).append($td4).append($td5);

							$("#tr_medTable").append($tr);

						}
					}
				}
		);
	});


	$("#save_dg_info").click(function (){
		var cr_ID = location.search.split('=')[1];
		//保存信息创建Diagnosis一行
		$.ajax({
					url:"/diagnosis/addDiagnosis",
					type:"POST",
					data:{
						cr_ID:cr_ID,
						dg_info:$("#dg_info").val(),
						med_id:[0]

					},
					async:false,
					success: function(result){
						if (result.state == 200){
							alert("保存成功");
						}else{
							alert(result.message);
						}

					}
				}
		);
	});

	// $("#save_med").click(function () {
	// 	alert("test");
	// 	//将添加的内容到数据库
	// 	//根据cr_id更新Diagnosis的数据
	// 	// TODO
	// 	$.ajax({
	// 		url:"/diagnosis/update_List",
	// 		type:"POST",
	// 		data:{
	// 			med_id_list:med_str_list,
	// 			cr_ID:cr_ID
	// 		},
	// 		success: function (rst){
	// 			if (rst.state == 200){
	// 				console.log(rst);
	// 			}
	// 		}
	// 	});
	// });
    </script>

</html>